package com.cibfintech.rpa.jdwzcx.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cibfintech.rpa.jdwzcx.service.JdwzcxService;

@Controller
public class JdwzcxController {

    @Autowired
    private JdwzcxService jdwzcxService;

    // @RequestMapping("/investigate")
    // public void investigate(String name, String cardNumber, String type) throws
    // Exception
    // {
    // HttpServletResponse response = ((ServletRequestAttributes)
    // RequestContextHolder.getRequestAttributes()).getResponse();
    //
    // response.setContentType("application/x-zip-compressed");
    // response.addHeader("Content-Disposition", "attachment; filename=result.zip");
    //
    // byte[] bytes = jdwzcxService.investigate(name, cardNumber, type);
    // OutputStream out = response.getOutputStream();
    // out.write(bytes);
    // out.flush();
    // }

    @GetMapping("/s")
    public ModelAndView s() {
        ModelAndView modelAndView = new ModelAndView("s");
        return modelAndView;
    }

    @PostMapping("/investigate")
    public ModelAndView investigate(String name, String cardNumber, String type, Authentication authentication) throws Exception {
        UserDetails userdetails = (UserDetails) authentication.getPrincipal();
        String receiver = userdetails.getUsername();
        jdwzcxService.investigate(receiver, name, cardNumber, type);
        ModelAndView model = new ModelAndView("redirect:/s");
        return model;
    }

}
